import requests as req;
import os;
import re;
from lxml import etree;

def get_list(url,params):

    count = 0;

    # UA伪装
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
        "Mobile-User-Agent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36"
    }


    file_path = "./bug_file/网易歌单/";
    if not os.path.exists(file_path):
        os.makedirs(file_path);


    result = req.get(url=url,headers=header);
    # result.encoding = "utf-8";
    html_text = result.text;
    html_obj = etree.HTML(html_text);

    lis = html_obj.xpath("//iframe[@id='g_iframe']//html/body/div[3]/div/ul/li[1]/div/a");
    # lis = html_obj.xpath("//div[@id='m-disc-pl-c']/div/ul[@id='m-pl-container']/li");

    ex = '<div class="u-cover u-cover-1">.*?</a>';
    tag = re.findall(ex,html_text,re.S)[0];

    print("TAG ==> ",html_text)
    for li in lis:
        try:
            # 歌单名字
            form_name = li.xpath("./div/a/@title")[0];
            # 歌单链接
            form_href = li.xpath("./div/a/@href")[0];
            form_href = "https://music.163.com/"+form_href;

            if not os.path.exists(file_path+form_name):
                os.makedirs(file_path+form_name);


            # 歌单中得列表
            html_text2 = req.get(url=form_href, headers=header).text;
            html_obj2 = etree.HTML(html_text2);
            href2 = html_obj2.xpath("//table[@class='m-table']/tbody/tr[@class='even']/td[2]//span[@class='txt']/a/@href")[0];
            # 歌曲名
            music_name = href2.xpath("./b/@title")[0];
            href2 = "https://music.163.com/" + href2;

            # 歌单中的某一首歌
            html_text3 = req.get(url=href2, headers=header).text;
            html_obj3 = etree.HTML(html_text3);
            href3 = html_obj3.xpath("//div[@class='cvrwrap f-cb f-pr']/div[@class='out s-fc3']//a[@class='des s-fc7']/@data-href")[0];

            # name.encode("iso-8859-1").decode("gbk");

            io = req.get(url=href3, headers=header).content;

            if io:
                with open(file_path+form_name+"/" + music_name + ".mp3", "wb") as wf:
                    wf.write(io);
                print(form_name+"-"+music_name+ ".mp3\t下载完成!!!");
                count +=1;
            else:
                print(f"\033[0;31;40m\t{form_name}-{music_name}.mp3\t下载失败!!!\033[0m");

        except Exception as e:
            print(f"\033[0;31;40m\t发生异常\t{form_name}-{music_name}.mp3\t下载失败!!!\033[0m", e);
            continue;
        else:
            # 没有发现异常
            pass;
        finally:
            # 不管是否出现异常都会执行
            pass;

    return count;


if __name__ == '__main__':


    url = "https://music.163.com/#/discover/playlist";

    # "https://music.163.com/#/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset=70"

    count = 0;

    for index in range(1000):
        index+=1;
        # print("index ===>>",index);

        if index > 1:
            params = {
                "order": "hot",
                "cat": "全部",
                "limit":35,
                "offset":index*35,
            };
        else:
            params = {};

        num = get_list(url,params);
        count += num;

        if num > 0:
            continue;
        else:
            break;


    print("爬取结束，本次成功爬取数据%d"%count+"条!!!");